package com.infosys.cabplanningdao.cab.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.infosys.cabplanning.common.domain.Cab;
import com.infosys.cabplanning.common.domain.CabType;
import com.infosys.cabplanningdao.cab.dao.CabServiceDAO;

public class CabServiceDAOImpl extends JdbcDaoSupport implements CabServiceDAO {

	public void saveCab(final List<Cab> cabList) {

		System.out.println("SaveCab values" + cabList.toString());
		String sql = "INSERT INTO hackathon.cab "
				+ "(type_id, registration) VALUES (?, ?)";
		
		System.out.println("Saving CAB Details");
		
		for (Cab cab : cabList) {
			if (cab.getCabType().getName().equalsIgnoreCase("indica")){
				cab.getCabType().setCabTypeID(1);
			} else {
				cab.getCabType().setCabTypeID(2);
			}
		}
//		String cabTypeSql = "SELECT type_id, cab_name from cab_type where name = ?";
//		CabType cabType = getJdbcTemplate().queryForObject(cabTypeSql, new CabTypeRowMapper());
		
		
		
		try {
			getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {

				@Override
				public void setValues(PreparedStatement ps, int i)
						throws SQLException {
					Cab cab = cabList.get(i);
					ps.setInt(1, cab.getCabType().getCabTypeID());
					ps.setString(2, cab.getRegistrationNo());
				}

				@Override
				public int getBatchSize() {
					return cabList.size();
				}
			});
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param custId
	 * @return
	 */
	public List<Cab> findCabs(Cab cabSearchDTO) {

		String sql = "SELECT cab_id, cab.type_id, name, capacity,"
				+ "registration FROM hackathon.cab cab, hackathon.cab_type cab_type WHERE cab_type.type_id = cab.type_id";
		try {
			String queryWhereConditions = "";
			List<Object> parameterList = new ArrayList<Object>();
			if (cabSearchDTO.getCabId() != 0) {
				queryWhereConditions = "AND cab_id = ? ";
				parameterList.add(cabSearchDTO.getCabId());
			}
			if (cabSearchDTO.getCabType() != null) {
				if (cabSearchDTO.getCabType().getCabTypeID() != 0) {
					queryWhereConditions = queryWhereConditions
							+ " AND cab.type_id = ?";
					parameterList.add(cabSearchDTO.getCabType().getCabTypeID());
				}
				if (cabSearchDTO.getCabType().getCapacity() != 0) {
					queryWhereConditions = queryWhereConditions
							+ " AND capacity = ?";
					parameterList.add(cabSearchDTO.getCabType().getCapacity());
				}
				if (cabSearchDTO.getCabType().getName() != null
						&& !cabSearchDTO.getCabType().getName().isEmpty()) {
					queryWhereConditions = queryWhereConditions + " AND name = ?";
					parameterList.add(cabSearchDTO.getCabType().getName());
				}
			}
			if (cabSearchDTO.getRegistrationNo() != null
					&& !cabSearchDTO.getRegistrationNo().trim().isEmpty()) {
				System.out.println("Registration S"+cabSearchDTO.getRegistrationNo()+"E");
				queryWhereConditions = queryWhereConditions
						+ " AND registration = ? ";
				parameterList.add(cabSearchDTO.getRegistrationNo());
			}

			if (!parameterList.isEmpty()) {
				System.out.println("Conditions : " + queryWhereConditions);
				sql = sql + queryWhereConditions;

				System.out.println("Query : " + sql);
				System.out.println("parameters : " + parameterList);

				List<Cab> cabList = getJdbcTemplate().query(sql,
						parameterList.toArray(), new CabRowMapper());
				return cabList;
			} else {
				// TODO Throw
				return new ArrayList<Cab>();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<Cab>();
	}

	/**
	 * 
	 */
	public List<Cab> findAll() {
		String sql = "SELECT cab_id, cab.type_id,  name, capacity,"
				+ "registration FROM hackathon.cab cab, hackathon.cab_type cab_type WHERE cab_type.type_id = cab.type_id";
		List<Cab> cabList = getJdbcTemplate().query(sql, new CabRowMapper());
		return cabList;
	}

}
